package com.cuitrip.business.comment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;

import com.cuitrip.apiservice.OrderBusiness;
import com.cuitrip.business.comment.ui.ReviewAdapter;
import com.cuitrip.business.tripservice.ServiceDetailActivity;
import com.cuitrip.service.R;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragmentActivity;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;
import com.lab.utils.MessageUtils;

import java.util.List;

/**
 * Created on 7/17.
 */
public class ViewReviewsActivity extends CustomUiFragmentActivity implements SwipeRefreshLayout.OnRefreshListener, IProxyCallback {
    private static final String TAG = "ViewReviewsActivity";
    private ListView mListView;
    private String sid;
    private ApiProxy apiProxy = new ApiProxy(this);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        if (intent == null || TextUtils.isEmpty(intent.getStringExtra(ServiceDetailActivity.SERVICE_ID))) {
            finish();
            return;
        }
        sid = intent.getStringExtra(ServiceDetailActivity.SERVICE_ID);
        super.onCreate(savedInstanceState, R.layout.ct_review_list);
    }

    @Override
    public void initView() {
        mListView = (ListView) findViewById(R.id.ct_list);
        showLoading();

    }

    @Override
    public void initData() {
        OrderBusiness.getReviewList(apiProxy, sid);
    }

    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onRefresh() {
//        refresh.setRefreshing(true);
//        OrderBusiness.getReviewList(this, mAsyncHttpClient, mHandler, sid);
//        findViewById(R.id.empty_view).setVisibility(View.VISIBLE);
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config = new CustomUiConfig();
        config.mTopbarLeftAreaName = getString(R.string.back_icon);
        config.mTopbarMiddleAreaName = getString(R.string.review_attribute_number_all);
        return config;
    }

    @Override
    public void onTopbarLeftPress() {
        finish();
    }

    @Override
    public boolean onProxyResult(ProxyResult<?> result) {
        CtApiResponse response = (CtApiResponse) result.getData();
        hideLoading();
        if (response.isResponseNormal()) {
            if (response.result instanceof List) {
                List<ReviewListItem> reviewList = (List<ReviewListItem>) response.result;
                if (reviewList != null && !reviewList.isEmpty()) {
                    ReviewAdapter adapter = new ReviewAdapter(ViewReviewsActivity.this, R.layout.ct_review_item);
                    adapter.setData(reviewList);
                    mListView.setAdapter(adapter);
                    findViewById(R.id.empty_view).setVisibility(View.GONE);
                    return true;
                }
                findViewById(R.id.empty_view).setVisibility(View.VISIBLE);
            }
        } else {
            hideLoading();
            if (!TextUtils.isEmpty(response.msg)) {
                MessageUtils.showToast(response.msg);
            }
        }
        return false;
    }
}
